package 数据结构.stack;

public class Calculator {
    public static void main(String[] args) {
        //完成表达式的运算
        String expression = "70+2*6-4";
        ArrayStack2 numStack = new ArrayStack2(10);
        ArrayStack2 operStack = new ArrayStack2(10);
        int index = 0;
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' ';
        String keepnum = "";//用于拼接多位数

        while (true) {
            //依次得到expression得到每个字符
            ch = expression.substring(index, index + 1).charAt(0);
            //判断ch具体是什么 数字 还是符号
            if (operStack.isOper(ch)) {
                //判断栈是否为空
                if (!operStack.ifEmpty()) {
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())) {
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);//吧结果入数栈
                        numStack.push(res);
                        //符号入栈
                        operStack.push(ch);
                    } else {
                        operStack.push(ch);
                    }
                } else {
                    operStack.push(ch);
                }
            } else {
                keepnum += ch;
                if (index == expression.length() - 1) {
                    numStack.push(Integer.parseInt(keepnum));
                } else {
                    if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
                        //满足就说明是运算符
                        numStack.push(Integer.parseInt(keepnum));
                        //清空keepnum
                        keepnum = "";
                    }
                }
            }
            index++;
            if (index >= expression.length()) {
                break;
            }
        }
        //
        while (true) {
            //如果符号栈为空,则计算到最后的结果,数栈中只有一个就是结果
            if (operStack.ifEmpty()) {
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);//吧结果入数栈
            numStack.push(res);
        }
        System.out.printf("表达式%s=%d", expression, numStack.pop());

    }
}

//需要扩展功能
class ArrayStack2 {
    private int maxSize;
    private int[] stack;
    private int top = -1;//表示栈顶

    public ArrayStack2(int maxSize) {
        this.maxSize = maxSize;
        stack = new int[maxSize];
    }

    public boolean ifFull() {
        return top == maxSize - 1;
    }

    public boolean ifEmpty() {
        return top == -1;
    }

    public void push(int value) {
        if (ifFull()) {
            System.out.println("栈满了");
            return;

        }
        top++;
        stack[top] = value;
    }

    //出栈
    public int pop() {
        if (ifEmpty()) {
            throw new RuntimeException("栈空,没有数据");
        }
        int value = stack[top];
        top--;
        return value;
    }

    //遍历栈,需要从栈顶开始显示数据
    public void list() {
        if (ifEmpty()) {
            System.out.println("栈空,没有数据");
            return;
        }
        for (int i = top; i >= 0; i--) {
            System.out.printf("stack[%d]=%d\n", i, stack[i]);
        }
    }

    //返回运算符的优先级,优先级是自己设定,优先级使用数字表示,数字越大表明优先级越高
    public int priority(int oper) {
        if (oper == '*' || oper == '/') {
            return 1;
        } else if (oper == '+' || oper == '-') {
            return 0;
        } else {
            return -1;
        }
    }

    //判断是不是一个运算符
    public boolean isOper(int val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    //计算方法
    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;

    }

    //增加一个方法,返回当期那栈顶的值
    public int peek() {
        return stack[top];
    }
}